package com.jxs.android.sxyw;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.CaseInfo;
import com.jxs.android.sxyw.data.UserInfo.Role;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

public class CaseDetail extends AbstractActivity implements OnClickListener {
	public static final int GET_DETAIL = 0x00989;
	private static final int PROCESS = 0x8876;

	private TextView work_order,// 工单号
			subject,// 主题
			des,// 任务描述
			end_time,// 处理时限
			area,// 所属地区
			business,// 业务
			company;// 代维公司
	TextView attachment;// 附件

	private LinearLayout case_info_work_order_ll, case_info_subject_ll, case_info_company_ll, case_info_attachment_ll, case_info_logs_ll;
	private CaseInfo caseInfo;
	private int position = 0;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.case_detail);

		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		case_info_work_order_ll = (LinearLayout) findViewById(R.id.case_info_work_order_ll);
		case_info_subject_ll = (LinearLayout) findViewById(R.id.case_info_subject_ll);
		case_info_company_ll = (LinearLayout) findViewById(R.id.case_info_company_ll);
		case_info_attachment_ll = (LinearLayout) findViewById(R.id.case_info_attachment_ll);
		case_info_logs_ll = (LinearLayout) findViewById(R.id.case_info_logs_ll);

		case_info_work_order_ll.setOnClickListener(this);
		case_info_company_ll.setOnClickListener(this);
		case_info_subject_ll.setOnClickListener(this);
		case_info_attachment_ll.setOnClickListener(this);
		case_info_logs_ll.setOnClickListener(this);

		work_order = (TextView) findViewById(R.id.case_info_work_order);
		subject = (TextView) findViewById(R.id.case_info_subject);
		des = (TextView) findViewById(R.id.case_info_task_des);
		end_time = (TextView) findViewById(R.id.case_info_end_time);
		area = (TextView) findViewById(R.id.case_info_area);
		business = (TextView) findViewById(R.id.case_info_business);
		company = (TextView) findViewById(R.id.case_info_company);
		attachment = (TextView) findViewById(R.id.case_info_attachment);

		initCaseInfo();
	}

	private void initCaseInfo() {
		if (null != getIntent() && null != getIntent().getExtras()) {
			caseInfo = (CaseInfo) getIntent().getExtras().getSerializable("caseInfo");
			position = getIntent().getExtras().getInt("position", 0);
		}

		if (null != caseInfo) {
			work_order.setText(caseInfo.getCase_no());
			subject.setText(caseInfo.getTitle());
			des.setText(caseInfo.getDescription());
			end_time.setText(caseInfo.getPlan_finish_time());

			area.setText(caseInfo.getCarrier_name());
			business.setText(caseInfo.getBusiness_name());
			company.setText(caseInfo.getCompany_name());
		}
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == case_info_subject_ll.getId()) {
			if (null != caseInfo) {
				requestDesDetail("主题", caseInfo.getTitle());
			}
		} else if (v.getId() == case_info_work_order_ll.getId()) {
			if (null != caseInfo) {
				requestDesDetail("工单号", caseInfo.getCase_no());
			}
		} else if (v.getId() == case_info_company_ll.getId()) {
			if (null != caseInfo) {
				requestDesDetail("代维公司", caseInfo.getCompany_name());
			}
		} else if (v.getId() == case_info_attachment_ll.getId()) {
			Bundle bundle = new Bundle();
			bundle.putSerializable("caseInfo", caseInfo);
			Intent intent = new Intent(CaseDetail.this, CaseAttachmentList.class);
			intent.putExtras(bundle);
			startActivity(intent);
		} else if (v.getId() == case_info_logs_ll.getId()) {
			Bundle bundle = new Bundle();
			bundle.putSerializable("caseInfo", caseInfo);
			Intent intent = new Intent(CaseDetail.this, CaseLogList.class);
			intent.putExtras(bundle);
			startActivity(intent);
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_detail);
		initTopBarReturn(topBar);
		topBar.setLogicBtnText("签收");
		topBar.setTopBarBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {
				if (null != SelfApplication.getUserInfo() && null != SelfApplication.getUserInfo().getRole()) {

					Bundle bundle = new Bundle();
					bundle.putSerializable("caseInfo", caseInfo);
					if (Role.DISPACHERR.ordinal() == SelfApplication.getUserInfo().getRole().ordinal()) {// 调度员
						if ("0".equals(caseInfo.getStatus().trim()) || "1".equals(caseInfo.getStatus().trim())) {
							Intent intent = new Intent(CaseDetail.this, CaseNextPerson.class);
							intent.putExtras(bundle);
							startActivityForResult(intent, PROCESS);
						} else if ("2".equals(caseInfo.getStatus().trim()) || "3".equals(caseInfo.getStatus().trim())) {
							Intent intent = new Intent(CaseDetail.this, CaseProcess.class);
							intent.putExtras(bundle);
							startActivityForResult(intent, PROCESS);
						} else {
							PopMsg.show("该工单状态不合法，不能处理！");
						}
					} else if (Role.MAINTAINER.ordinal() == SelfApplication.getUserInfo().getRole().ordinal()) {// 代维人员
						if ("2".equals(caseInfo.getStatus().trim()) || "3".equals(caseInfo.getStatus().trim())) {
							Intent intent = new Intent(CaseDetail.this, CaseProcess.class);
							intent.putExtras(bundle);
							startActivityForResult(intent, PROCESS);
						} else {
							PopMsg.show("该工单状态不合法，不能处理！");
						}
					}
				}
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			resultOk();
		}
	}

	private void resultOk() {
		PopMsg.show("处理成功！");
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putInt("position", position);
		intent.putExtras(bundle);
		setResult(RESULT_OK, intent);
		this.finish();
	}
}
